<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UploadController extends Controller
{
    public function save(Request $request)
    {
        $file = $request->file('file');
        if (!$request->hasFile('file')) {
            return self::response(null,-1,'请选择要上传的文件哦~');
        }
        if (!$file->isValid()) {
            return self::response(null,-2,'文件上传出错了哦~');
        }
        $type = str_before($file->getMimeType(),'/');
        $size = $file->getSize();
        if ($type === 'image') {
            if ($size > 10*1024*1024) {
                return self::response(null,-3,'图片大小不可以超过10M哦~');
            }
            $dir = 'upload/img';
        } elseif ($type === 'video') {
            if ($size > 100*1024*1024) {
                return self::response(null,-4,'视频大小不可以超过10M哦~');
            }
            $dir = 'upload/video';
        } else {
            return self::response(null,-5,'您的文件格式不合法哦~');
        }
        $path = $file->store($dir,'public');
        $url  = asset('storage/'.$path);
        return self::response(['url' => $url],200);
    }
}
